<?php

class VideoLinkManager
{
	private $_link_string;
	private $_link = null;
	private $_thumbnail = null;
	private $_title = null;
	
	public function __construct($links_string)
	{
		$this->_link_string = $links_string;
		
		if(strpos($this->_link_string, 'youtube'))
			$this->checkYoutube();
		elseif(strpos($this->_link_string, 'vimeo'))
			$this->checkVimeo();
	}
	
	public function checkVimeo()
	{
		if(!$this->_link_string)
			return;
		
		$groups = array();
		ereg('.*clip_id=([^\&]*)', $this->_link_string, $groups);	// try to find in link
		$id = $groups[1];
			
		if(empty($id))
			return;
			
		$this->_link		= 'http://vimeo.com/moogaloop.swf?clip_id='.$id;
	}
	
	public function checkYoutube()
	{
		if(!$this->_link_string)
			return;
		
		$groups = array();
		ereg('.*v=([^\&]*)', $this->_link_string, $groups);	// try to find in link
		$id = $groups[1];
			
		if(empty($id))
		{
			$groups = array();
			ereg('.*v/([^\&]*)', $this->_link_string, $groups);	// try to find in embedded
			$id = $groups[1];
			
			if(empty($id))
				return;
		}
				
		$xml = new XMLStrip;				
		$datalink = 'http://gdata.youtube.com/feeds/api/videos/'.$id;
			
		/* Prerare data for saving */	
		$pure = file_get_contents($datalink);
		if(empty($pure))
			return;
			
		$data				= ($xml->xml2ary($pure));
		$this->_link		= 'http://www.youtube.com/v/'.$id;
		$this->_title		= $data['entry']['_c']['media:group']['_c']['media:title']['_v'];
		$this->_thumbnail	= $data['entry']['_c']['media:group']['_c']['media:thumbnail']['0']['_a']['url'];
	}
	
	public function getLink()
	{
		return $this->_link;
	}
	
	public function getThumbnail()
	{
		return $this->_thumbnail;
	}
	
	public function getTitle()
	{
		return $this->title;
	}
}

?>